package com.example.datajpatable.service.impl;
import com.example.datajpatable.domain.Student;
import com.example.datajpatable.exception.REnum;
import com.example.datajpatable.exception.RException;
import com.example.datajpatable.repository.StudentRepository;
import com.example.datajpatable.service.StudentService;
import org.aspectj.weaver.patterns.ExactTypePattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentimplService implements StudentService {
    @Autowired
    private StudentRepository studentRepository;


    public Page<Student> getUserList(int pageNum, int pageSize) {
        Sort.Order order=new Sort.Order(Sort.Direction.ASC, "id");
        Pageable pageable = PageRequest.of(pageNum, pageSize, Sort.by(order));
        Page<Student> users = studentRepository.findAll(pageable);
        return users;
    }



    public List<Student> findAll(){
        return studentRepository.findAll();
    }

    /**
     * 只做分页的查询
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Pageable pageable){
        return studentRepository.findAll(pageable);
    }

    /**
     * 按学生生成的一个案例查询
     * @param studentExample
     * @return
     */
    public List<Student> findAll(Example<Student> studentExample){

        return studentRepository.findAll(studentExample);
    }

    public Page<Student> findAll(Example<Student> student,Pageable pageable){
//        if(student == null){
//            return studentRepository.findAll(pageable);
//        }else{
//            ExampleMatcher matcher = ExampleMatcher.matching()
//                    .withMatcher("sname",m -> m.startsWith());
//            Example<Student> studentExample = Example.of(student,matcher);
//            return  studentRepository.findAll(studentExample,pageable);
//        }
        return studentRepository.findAll(student,pageable);
    }

    /**
     * 按id进行查询
     * @param id 主键，整数
     * @return
     */
    public Student findById(Integer id){

        Student student =studentRepository.findById(id).orElse(null);
        return student;
    }


    /**
     * 按名字进行查询
     * @param name
     * @return
     */
    public List<Student> findBySname(String name){
        return studentRepository.findBySnameLike(name);
    }



//    public List<Student> findBySnameAndSno(String name,String sno){
//
//        return studentRepository.findBySnameAndSno(name,sno);
//    }

    public Student insert(Student student){
        studentRepository.save(student);
        return student;
    }

    public Student update(Student student){

        return studentRepository.save(student);
    }

//    public void delete(Integer id){
//        studentRepository.deleteById(id);
//    }
//    public void delete(Student student){
//        studentRepository.delete(student);
//    }

    public void delete(Integer id){
        Student student = new Student();
        student.setId(id);
        studentRepository.delete(student);
    }

    public Student findBySnameAndSno(String name,String sno) throws Exception{
        List<Student> bySnameAndSno = studentRepository.findBySnameAndSno(name,sno);
        if(bySnameAndSno.size() > 0){
            return bySnameAndSno.get(0);
        }
        else{
            throw new RException(REnum.LOGIN_ERR);
        }
    }

    public List<Student> findBySnameAndPassword(String name,String password){
         return studentRepository.findBySnameAndPassword(name,password);
    }
}
